// @ts-nocheck
export default class RouteUtil {
  constructor (routeId, routeTabData, allPatternList) {
    this.routeId = routeId
    this.routeData = routeTabData
    this.allPatternList = allPatternList
  }
  getAddFeatute (agentid, patternId) {
    let addPropertity = {
      barriers: [],
      stages: [],
      stagesList: []
    }
    for (let pdev of this.allPatternList) {
      if (pdev.agentid === agentid) {
        let pPatternList = pdev.feature.patternList
        let pPattern = pPatternList.find(pItem => pItem.id === patternId)
        if (pPattern) {
          addPropertity = {
            barriers: pPattern.barriers,
            stages: pPattern.stages,
            stagesList: pPattern.stagesList
          }
        }
        break
      }
    }
    return addPropertity
  }
  getGenerateParam (routeData, bizType = 0) { // bizType： 1 generate专用
    let route = JSON.parse(JSON.stringify(routeData))
    // let targetPlanList = []
    let param = {
      'id': route.id,
      'name': route.name,
      'keyintsid': route.keyintsid,
      'direction': route.direction,
      'optstrategy': route.optstrategy,
      'upspeed': route.upspeed,
      'downspeed': route.downspeed,
      'type': route.type ? route.type : 0,
      'devs': []
    }
    let devs = route.devs
    let targetDevs = []
    for (let inter of devs) {
      let patternList = inter.patternList.map(item => {
        let pattern = {
          'id': item.id,
          'desc': item.desc,
          'offset': item.offset,
          'cycle': item.cycle,
          'contrloType': item.contrloType,
          'special': item.special,
          'rings': item.rings,
          'stages': item.stages,
          'stagesList': item.stagesList,
          'barriers': item.barriers
        }
        if (item.contrloType === 'stage') {
          pattern.rings = []
        }
        // add some feature
        let addPropertity = this.getAddFeatute(inter.agentid, item.id)
        pattern = {...pattern, ...addPropertity}
        return pattern
      })
      let phaseList = inter.phaseList.map(item => {
        // let pattern = {
        //   'id': item.id,
        //   'concurrent': item.concurrent
        // }
        let pattern = {...item}
        return pattern
      })
      let feature = {
        patternList: patternList,
        phaseList: phaseList
      }
      inter.routeid = param.id
      if (!inter.feature || !inter.feature.phaseList || !inter.feature.patternList || inter.patternid !== inter.patternId || inter.patternList.length !== 1) {
        inter.feature = feature
      }
      delete inter.id
      delete inter.name
      delete inter.allPatterns
      delete inter.currPhase
      delete inter.phaseList
      targetDevs.push(inter)
    }
    param.devs = targetDevs
    // param = route
    param.devs = param.devs.map((item, index) => {
      let dev = {
        'agentid': item.agentid,
        'routeid': item.routeid,
        'forwardphaseid': item.forwardphaseid,
        'backphaseid': item.backphaseid,
        'distance': item.distance,
        'width': item.width,
        'sortid': item.sortid ? item.sortid : index + 1,
        'patternid': item.patternid,
        'feature': item.feature,
        'fixedtimeplantype': item.fixedtimeplantype,
        'phases': item.phases,
        'upspeed': item.upspeed,
        'downspeed': item.downspeed
      }
      if (bizType === 1) {
        if (item.patternid !== item.patternId || item.patternList.length !== 1) { // change paternId
          dev.feature.patternList = dev.feature.patternList.filter(pattern => pattern.id === dev.patternid)
        }
      }
      delete dev.patternList
      return dev
    })
    return param
  }
}
